<template>
	<gui-page ref="guiPage" :customHeader="true">
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('translationImage.incomingInspection')"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="distributed">
				<view class="gui-bg-white gui-dark-bg-level-3 gui-padding-x distributed-head">
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text" style="width:300rpx">{{$t('purchaseReturn.generateOrder')}}</text>
						<view class="gui-form-body gui-text-right">
							<input type="text" class="gui-form-input gui-text-right" v-model="data.oddNum"
							 :placeholder="$t('distributedEnter.placeholderText')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text" style="width:200rpx">{{$t('purchaseReturn.generateDate')}}</text>
						<view class="gui-form-body gui-text-right" @click="dateChoose">
							<view class="gui-form-input gui-text-right" v-if="data.chooseTime.length">
								<text>{{data.chooseTime[0]}}</text><text v-if="data.chooseTime[1]"> - {{data.chooseTime[1]}}</text>
							</view>
							<input v-else type="button" class="gui-form-input gui-text-right"
								:placeholder="$t('distributedEnter.placeholderTextChoose')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text" style="width:300rpx">{{$t('stockManage.inspectionStatus')}}</text>
						<view class="gui-form-body gui-text-right" style="overflow: visible;">
							<superweiCombox style="text-align: right;" :candidates="data.selectList" :placeholder="$t('stockManage.placeholder')"
							:isJSON="true" v-model="data.name" keyName="name" :defaultValue="$t('smashRemoval.all')" @select="select"></superweiCombox>
						</view>
					</view>
					<view class="gui-form-item gui-border-b gui-flex gui-justify-content-center">
						<view class="gui-flex gui-space-between gui-align-items-center"
							style="width: 70%;height: 100rpx;">
							<button type="default" class="gui-button gui-button-mini gui-bg-black4 gui-noborder"
								style="width:200rpx;" @click="reset">
								<text
									class="gui-block gui-color-black gui-button-text-mini gui-icons">{{$t('distributedEnter.reset')}}</text>
							</button>
							<button type="default" class="gui-button gui-button-mini gui-bg-blue gui-noborder"
								style="width:200rpx;" @click="handelsearch(OPERATION_SEARCH)">
								<text
									class="gui-block gui-color-white gui-button-text-mini gui-icons">{{$t('distributedEnter.search')}}</text>
							</button>
						</view>
					</view>
				</view>
				<!-- 底部显示区域 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll" @scrolltolower="scrolltolower">
					<view style="height: 15rpx"></view>
					<view class="card-boxCustom gui-transition-all" v-for="(x,i) in data.list" @click="goPage(x)" :key="i">
						<view class="card-head" style="">
							<text>{{$t('purchaseReturn.generateOrder')}}：{{x.instockCode}}</text>
							<uni-tag v-if="x.checkStatus == 'checked'" :inverted="true" :text="$t('translationImage.checked')" type="success" />
							<uni-tag v-else-if="x.checkStatus == 'waitCheck'" :inverted="true" :text="$t('translationImage.checkedNo')" type="primary" />
						</view>
						<view class="card-body" style="padding-bottom: 15rpx;">
							<view class="card-rotating" @click.stop="data.listShow[i] = !data.listShow[i]">
								<uni-icons type="bottom" color="#9C9C9C" size="20"></uni-icons>
							</view>
							<view class="card-body-item">
								<view class="card-item-50">
									<text class="body-item-title">{{$t('purchaseReturn.generateDate')}}：</text>{{x.instockTime}}
								</view>
								<view class="card-item-50">
									<text class="body-item-title">{{$t('translationImage.supplier')}}：</text> {{x.supplierName}}
								</view>
							</view>
							<view class="card-body-item">
								<view class="card-item-50">
									<text class="body-item-title">{{$t('translationImage.materialNape')}}：</text> {{x.materialTotal}}
								</view>
								<view class="card-item-50">
									<text class="body-item-title">{{$t('distributedEnter.compiler')}}：</text> {{x.createUserName}}
								</view>
							</view>
						</view>
						<view class="card-body-folding" v-show="data.listShow[i]">
							<view class="gui-border-t" style="padding: 20rpx 20rpx 0 20rpx;" v-for="(k,index) in x.materialGroups" :key="index">
								<view class="card-body-item">
									<text class="body-item-title">{{$t('distributedEnter.materialCode')}}：</text> {{k.materialCode}}
								</view>
								<view class="card-body-item">
									<text class="body-item-title">{{$t('distributedEnter.materialName')}}：</text> {{k.materialName}}
								</view>
								<view class="card-body-item">
									<text class="body-item-title">{{$t('distributedEnter.materialSpec')}}：</text> {{k.materialSpec}}
								</view>
								<view class="card-body-item">
									<text class="body-item-title">{{$t('distributedEnter.materialQty')}}：</text> {{k.qty}}
								</view>
							</view>
						</view>
					</view>
					<view v-if="data.noticeHooks" class="noticeHooks">
					    {{data.list.length == 0 ? $t('stockManage.notAvailable') : $t('stockManage.without')}}
					</view>
				</scroll-view>
			</view>
			<calendarCustom ref="calendarCustomRef" @setTime="getTime" :selectTime="data.chooseTime">
			</calendarCustom>
		</template>
	</gui-page>
</template>
<script setup>
	import searchCustom from '@/components/searchCustom';
	import calendarCustom from '@/components/calendarCustom';
	import { onShow } from '@dcloudio/uni-app';
	import { basPdaOperationLogFn } from '@/utils/index.js';
	import { OPERATION_SEARCH, OPERATION_REASCT, OPERATION_LOOK, OPERATION_CHOOSE } from '@/constant/index.js'
	import {
		ref,
		reactive,
		onMounted,
		getCurrentInstance
	} from 'vue';
	import graceJS from "@/Grace6/js/grace.js";
	import {
		navigate
	} from '@/utils/routerSkip.js';
	import { 
		getIncomingMaterial
	} from '@/api/api.js'
	import {
		useStore
	} from "vuex";
	import superweiCombox from '@/components/superweiCombox';
	//JS国际化引入
	import { useI18n } from 'vue-i18n';
	const { t } = useI18n();

	components: {
		searchCustom,
		calendarCustom,
		superweiCombox
	}
	let store = useStore();
	let calendarCustomRef = ref(null);
	let data = reactive({
		name:t('smashRemoval.all'),
		selectList: [{
			id: 'waitCheck',
			name: t('translationImage.checkedNo')
		}, {
			id: 'checked',
			name: t('translationImage.checked')
		}, {
			id: '',
			name: t('smashRemoval.all')
		}],
		chooseTime: [],
		oddNum: '',
		selectId:'',
		list: [],
		listShow:[],
		operationType:'', // 操作日志操作类型
		noticeHooks: false, //暂无相关数据标识
		page:1,
		rows:10,
	});
	const {
		ctx
	} = getCurrentInstance();
	onMounted(() => {
		// getSystemTimeRequest()
		// search()
	})
	onShow( async () => {
		getSystemTimeRequest()
	})
	const getSystemTimeRequest = async () => {
		let time = await store.dispatch('getSystemTimeRequest') || ''
		data.chooseTime.push(time)
		search(OPERATION_LOOK)
        console.log(time,'----time----')
	}
	const select = (e) => {
		console.log(e.id);
		console.log(e.name);
		data.selectId = e.id;
	}
	const goPage = (x) => {
		if(x.checkStatus == 'checked') {
			uni.showToast({
				title: t('materials.documentsHaveBeen'),
				icon: "none"
			});
			return
		}
		try {
			let inputParameter = x.instockCode
			let returnParameter = '无'
			let operationPicture = '来料检验'
			let callInterface = '无'
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode:'无',//返回状态
				errorMessage:'无',//错误信息
				isPass:'Y',//是否成功 Y N
				operationType:OPERATION_CHOOSE
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
			
		}
		navigate({
			url: `/pages/qms/iqcDetail?instockCode=${x.instockCode}`
		})
	}
	//上拉加载
	const scrolltolower = () => {
		if(!data.noticeHooks){
			data.page ++
			search(data.operationType)
		}
	}
	const handelsearch = (type) =>{
		data.page = 1
		search(type)
	}
	const search = (type) => {
		data.operationType = type
		let parmes = {
			page:data.page,
			rows:data.rows,
			isSp:0,// 代表是否是五金仓
			instockTimeStart:data.chooseTime[0] || '',
			instockTimeEnd:data.chooseTime[1] ? data.chooseTime[1] : (data.chooseTime[0] || ''),
			checkStatus:data.selectId,
			instockCode:data.oddNum,
		}
		getIncomingMaterial(parmes).then(res=>{
			try {
				let inputParameter = JSON.stringify(parmes)
				let returnParameter = JSON.stringify(res)
				let operationPicture = '来料检验'
				let callInterface = res.rquestUrl
				let opertionParmes = {
					inputParameter,
					returnParameter,
					operationPicture,
					callInterface,
					responseCode:res.status,//返回状态
					errorMessage:res.message,//错误信息
					isPass:res.status == 200 ? 'Y' : 'N',//是否成功 Y N
					operationType:type
				}
				basPdaOperationLogFn(opertionParmes)
			} catch (error) {
			}
			if(res.status == 200){
				// data.noticeHooks = false
				// if (res.data.length == 0) {
				// 	data.noticeHooks = true
				// }
				// data.list = res.data;
				// data.listShow = [];
				// data.list.forEach(x=>{
				// 	listShow.push(false)
				// })


				let arr = res.data.rows
				let listShow = []
				arr.forEach(x=>{
					listShow.push(false)
				})
				data.noticeHooks = false
				if (data.page === 1) {
					data.list = arr
					data.listShow = listShow
				} else {
					data.list = [...data.list, ...arr]
					data.listShow = [...data.listShow, ...listShow]
				}
				data.noticeHooks = data.list.length >= res.data.total
			}
		})
	}
	const reset = () => {
		data.chooseTime = []
		data.oddNum = ''
		data.name = t('smashRemoval.all')
		data.selectId = ''
		data.page = 1
		search(OPERATION_REASCT)
	}
	const dateChoose = () => {
		calendarCustomRef.value.open();
	}
	const getTime = (times) => {
		data.chooseTime = times;
	}
</script>
<style lang="scss" scoped>
	.body-item-title{
		// width: 140rpx!important;
	}
	.distributed-head {
		border-bottom-left-radius: 16rpx;
		border-bottom-right-radius: 16rpx;
	}

	.scroll {
		flex: 1;
		height: 2rpx;
	}

	.distributed {
		display: flex;
		flex-direction: column;
		height: calc(100vh - 88rpx);
		// height: 100vh;
	}

	.card-box {
		margin: 0 15rpx;
		box-shadow: 0rpx 0rpx 4rpx 0 #A5A5A5;
		background: #fff;
		padding: 30rpx;
		border-radius: 16rpx;
	}

	.card-item {
		line-height: 60rpx;
	}

	.card-bottom {
		margin-bottom: 30rpx;
	}

	.filtrate-item {
		display: flex;
		align-items: center;
		font-size: 32rpx;
		margin-bottom: 20rpx;

		.filtrate-itemLeft {
			height: 32rpx;
			line-height: 32rpx;
			width: 100rpx;
		}

		.filtrate-itemRight {
			border: 2rpx solid #A5A5A5;
			border-radius: 8rpx;
			overflow: hidden;
			line-height: 40rpx;
			width: 300rpx;
		}
	}
</style>
